#include "MCU_include.h"


unsigned char OVP_Calibration(volatile unsigned char *OVP_n,unsigned char DAC_Vaule)
{
	unsigned char Calibration0 = 0,Calibration1 = 0;
	unsigned char Reg_Buffer = 0;
	unsigned char i = 0;
	bit OVP_init_OUT = 0;
  
	if(OVP_n == OVP1)
	{
		OVP_n+=2;
		*OVP_n = 0x82;
		OVP_n-=2;
	}
	OVP_n+=3;
	*OVP_n = DAC_Vaule;
  
	OVP_n-=3;
	*OVP_n = 0b00100000;
  
	OVP_n+=1;
	*OVP_n = 0b01000000;
	Reg_Buffer = *OVP_n;
	OVP_init_OUT = (Reg_Buffer & 0x80)>>7; 
	for(i=0;i<32;i++)
	{
		*OVP_n = 0b01000000+i;
		GCC_DELAY(1000);
		Reg_Buffer = *OVP_n;
		if(OVP_init_OUT != ((Reg_Buffer & 0x80)>>7))
		{
			Calibration0 = i;
			break; 	
		}      	
	}
	*OVP_n = 0b01011111;
  
	Reg_Buffer = *OVP_n;
  
	OVP_init_OUT = (Reg_Buffer & 0x80)>>7; 
  
	for(i=0;i<32;i++)
	{
		*OVP_n = 0b01011111-i;
		GCC_DELAY(1000);
		Reg_Buffer = *OVP_n;
		if(OVP_init_OUT != ((Reg_Buffer & 0x80)>>7))
		{
			Calibration1 = (31-i);
			break; 	
		}      	
	}
	*OVP_n = 0x00;
	OVP_n-=1;
	*OVP_n = 0x00;
	OVP_n +=3;
	*OVP_n = 0x00;
	OVP_n -=1;
	*OVP_n = 0x00;
	return (Calibration0+Calibration1)/2;	
}

void OVP_SW(volatile unsigned char *OVP_n,unsigned char  SW_Status)
{	
	unsigned char Reg_Buffer = 0;
	Reg_Buffer = *OVP_n;
	Reg_Buffer &= 0b11011111;
	if(SW_Status == 1)
	{
		Reg_Buffer += 0b00100000;
	}
	*OVP_n = Reg_Buffer; 	
}


void OVP_02345_Init(volatile unsigned char *OVP_n,unsigned char DAC_Vaule,unsigned char debounce_time,unsigned char Hysteresis_voltage,unsigned char debounce_out_polarity,unsigned char Calibration_value)
{
	if((OVP_n != OVP1) && (OVP_n != OVP6))
	{
		*OVP_n = (debounce_out_polarity+debounce_time);
		OVP_n+=3;
		*OVP_n =  DAC_Vaule;	
		OVP_n-=1;
		*OVP_n = Hysteresis_voltage;
		OVP_n-=1;
		*OVP_n = (Calibration_value & 0x1F); 
	}		
}
void OVP_0_Init(unsigned char  OVP_N_Sel,unsigned char DAC_Vaule,unsigned char debounce_time,unsigned char Hysteresis_voltage,unsigned char debounce_out_polarity,unsigned char Calibration_value)
{
	_ovp1c0 = debounce_time+debounce_out_polarity;
	if(OVP_N_Sel == OVP1_N_DAC_IN)
	{
		_ovp0c2 = 0b10000010 + Hysteresis_voltage;
	}else _ovp0c2 = 0b00000001 + Hysteresis_voltage;
	_ovp0da =  DAC_Vaule; 
	_ovp0c1 = (Calibration_value & 0x1F);
}

void OVP_1_Init(unsigned char  OVP_N_Sel,unsigned char DAC_Vaule,unsigned char debounce_time,unsigned char Hysteresis_voltage,unsigned char debounce_out_polarity,unsigned char Calibration_value)
{
	_ovp1c0 = debounce_time+debounce_out_polarity;
	if(OVP_N_Sel == OVP1_N_DAC_IN)
	{
		_ovp1c2 = 0b10000010 + Hysteresis_voltage;
	}else _ovp1c2 = 0b00000001 + Hysteresis_voltage;
	_ovp1da =  DAC_Vaule; 
	_ovp1c1 = (Calibration_value & 0x1F);
}

void OVP_6_Init(unsigned char  OVP_P_Sel,unsigned char DAC_Vaule,unsigned char debounce_time,unsigned char Hysteresis_voltage,unsigned char debounce_out_polarity,unsigned char Calibration_value)
{
	_ovp6c0 = debounce_time+debounce_out_polarity;
	if(OVP_P_Sel == OVP6_P_IN0)
	{
		_ovp6c2 = 0b00000001 + Hysteresis_voltage;
	}else _ovp6c2 = 0b00000010 + Hysteresis_voltage;
	_ovp6da =  DAC_Vaule; 
	_ovp6c1 = (Calibration_value & 0x1F);	
}

void OVP_7_Init(unsigned char  OVP_P_Sel,unsigned char DAC_Vaule,unsigned char debounce_time,unsigned char Hysteresis_voltage,unsigned char debounce_out_polarity,unsigned char Calibration_value)
{
	_ovp7c0 = debounce_time+debounce_out_polarity;
	if(OVP_P_Sel == OVP7_P_IN0)
	{
		_ovp7c2 = 0b00000001 + Hysteresis_voltage;
	}else _ovp7c2 = 0b00000010 + Hysteresis_voltage;
	_ovp7da =  DAC_Vaule; 
	_ovp7c1 = (Calibration_value & 0x1F);	
}


/******** OPA Calibration function *********/
unsigned char OPA_Calibration()
{
	unsigned char calibration0 = 0,calibration1 = 0;
	unsigned char Output_Status = 0;
	unsigned char i = 0;
	_opoofm = 1;
	_oporsp = 1;
	_open = 1;
	
	_opocal &= 0b11000000;
	Output_Status = _opo;
	GCC_DELAY(1000);
	for (i=0;i<64;i++)
	{
		if (Output_Status == _opo)
		{
			_opocal = _opocal+1;
			GCC_DELAY(1000);
		}
		else
		{
			calibration0 = _opocal & 0b00111111;
			break;
		}
	}
	
	_opocal |= 0b00111111;
	Output_Status = _opo;
	GCC_DELAY(1000);
	for (i=0;i<64;i++)
	{
		if (Output_Status == _opo)
		{
			_opocal = _opocal-1;
			GCC_DELAY(1000);
		}
		else
		{
			calibration1 = _opocal & 0b00111111;
			break;
		}
	}
	return ((calibration0+calibration1)>>1);		
}


/********************/
void OPA_Init(unsigned char  Phase, unsigned char  RatioDouble, unsigned char R2_than_R1, unsigned char temp_Calibration_Value)
{
	if (Phase == 0)			//
		_opc0 = 0b00000110;
	else if(Phase == 1)
		_opc0 = 0b00000101;	//- - - - OPS3 OPS2 OPS1 OPS0	
		else if(Phase == 2)
		_opc0 = 0b00001001;	//
		
	if (RatioDouble == 0)
		_opg2xen = 0;
	else
		_opg2xen = 1;
	
	_opc1 &= 0b11111000;
	_opc1 |= R2_than_R1;	//OPEN — — OPO OPG2XEN OPG2 OPG1 OPG0
				
	_opocal &= 0b11000000;
	_opocal |= temp_Calibration_Value;
	_opoofm = 0;		
	_oporsp = 0;	
}


void OPAInit()
{	
	_opc0=0b00000101;		//- - - - OPS3 OPS2 OPS1 OPS0	
	_opc1=0b10001100;		//OPEN — — OPO OPG2XEN OPG2 OPG1 OPG0	
	_opoofm=0;				//			
}

/******************* End of  file ***************/